<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <script>
        function Validate(){
            var txtName=document.getElementById("txt_name");
            var button=document.getElementById("btnValidate");
            var form=document.getElementById("testform");
            if(txtName.value.trim()==""){
                var label=document.createElement("label");
                label.setAttribute("for","txt_name");
                form.insertBefore(label,button);
                txtName.labels[1].innerHTML="请输入姓名";
                txtName.labels[1].setAttribute("style","font-size:9px;color:red");
            }
        }
    </script>
    <form id="testform">
        <label id="label" for="txt_name">姓名：</label>
        <input id="txt_name">
        <input type="button" id="btnValidate" value="验证" onclick="Validate()">

    </form>
</body>
</html>